﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Text;
using LdtJudge.DataEngine.Model.XMLConfigModel.DataStores;
using Newtonsoft.Json;

namespace LdtJudge.DataEngine.Common
{
    /// <summary>
    /// 数据操作进行扩展
    /// </summary>
   public static class BaseExtensions
    {
        ///// <summary>
        ///// 进行数据对象的复制
        ///// </summary>
        ///// <typeparam name="T">数据对象类型</typeparam>
        ///// <param name="RealObject"></param>
        ///// <returns></returns>
        //public static T Clone<T>(T RealObject) where T : class
        //{
        //    using (Stream objectStream = new MemoryStream())
        //    {
        //        //利用 System.Runtime.Serialization序列化与反序列化完成引用对象的复制
        //        IFormatter formatter = new BinaryFormatter();
        //        formatter.Serialize(objectStream, RealObject);
        //        objectStream.Seek(0, SeekOrigin.Begin);
        //        return (T)formatter.Deserialize(objectStream);
        //    }
        //}
        /// <summary>
        /// 进行数据对象的复制
        /// </summary>
        /// <typeparam name="T">数据对象类型</typeparam>
        /// <param name="RealObject"></param>
        /// <returns></returns>
        public static DataModel Clone(this DataModel model)
        {
            if (model.sourceData==null)
            {
                return null;
            }
            else
            {
                DataModel dataModel = JsonConvert.DeserializeObject<DataModel>(JsonConvert.SerializeObject(model));
                return dataModel;
            }
            
        }
    }
}
